import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/index',
      component: () => import('@/views/Index.vue')
    },
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: () => import('@/views/UserLogin.vue')
    },
    {
      path: '/notFound',
      component: () => import('@/views/error/errorHtml404.vue')
    },
    {
      path: '/:pathMatch(.*)',
      redirect: '/notFound'
    }
  ]
})

router.beforeEach((to) => {
  const userStore = useUserStore()

  // userStore.setToken('qwe')
  console.log(userStore.token)
  if (!userStore.token && to.path !== '/login' && to.path !== '/notFound') return '/login'
})

export default router
